package com.itttq.gen.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itttq.gen.common.constant.StatusTypeEnums;
import com.itttq.gen.model.po.AccountPo;
import com.itttq.gen.model.qo.AccountQo;
import com.itttq.gen.model.to.ResultTo;
import com.itttq.gen.mapper.IAccountMapper;
import com.itttq.gen.utils.SecretUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
@Slf4j
public class AccountService {
    @Autowired
    private IAccountMapper accountMapper;

    /**
     * 根据ID查询对象信息
     *
     * @param id
     */
    public AccountPo getAccount(long id) {
        return accountMapper.selectById(id);
    }

    /**
     * 根据ID删除对象
     *
     * @param id
     */
    public void delAccount(long id) {
        int rs = accountMapper.deleteById(id);
        Assert.isTrue(rs > 0, ResultTo.getCodeAndMsg(ResultTo.ResultCode.FAIL, "删除数据失败"));
    }

    /**
     * 根据ID集合批量删除对象
     *
     * @param ids ID的List集合
     */
    public void delAccountByIds(List<Long> ids) {
        int rs = accountMapper.deleteBatchIds(ids);
        Assert.isTrue(rs > 0, ResultTo.getCodeAndMsg(ResultTo.ResultCode.FAIL, "批量删除数据失败"));
    }

    /**
     * 新增对象
     *
     * @param account 新增的PO对象
     */
    public void addAccount(AccountPo account) {
        int rs = accountMapper.insert(account);
        Assert.isTrue(rs > 0, ResultTo.getCodeAndMsg(ResultTo.ResultCode.FAIL, "新增数据失败"));
    }

    /**
     * 根据ID更新对象，对象的ID值必须传入
     *
     * @param account 更新的PO对象，必传ID值
     */
    public void updateAccount(AccountPo account) {
        int rs = accountMapper.updateById(account);
        Assert.isTrue(rs > 0, ResultTo.getCodeAndMsg(ResultTo.ResultCode.FAIL, "更新数据失败"));
    }

    /**
     * 根据条件查询记录数
     *
     * @param accountQo 查询对象
     * @return int 记录总数
     */
    public int getAccountCount(AccountQo accountQo) {
        return accountMapper.selectCount(getQw(accountQo));
    }

    /**
     * 获取所有数据列表，不分页
     *
     * @param accountQo 查询对象
     * @return List<AccountPo>
     */
    public List<AccountPo> getAccounts(AccountQo accountQo) {
        return accountMapper.selectList(getQw(accountQo));
    }

    /**
     * 获取所有数据列表，并分页
     *
     * @param accountQo 查询对象
     * @return IPage<AccountPo>
     */
    public IPage<AccountPo> getAccountAndPage(AccountQo accountQo) {
        return accountMapper.selectPage(new Page<>(accountQo.getPageNo(), accountQo.getPageSize()), getQw(accountQo));
    }

    /**
     * 构建Mybatis-Plus的查询条件对象。
     * TODO 此处需要自行根据需要构建
     *
     * @param accountQo 查询对象
     * @return LambdaQueryWrapper<AccountPo>
     */
    private LambdaQueryWrapper<AccountPo> getQw(AccountQo accountQo) {
        return new QueryWrapper<AccountPo>().lambda();
    }

    /**
     * 用户登录
     *
     * @param username 用户名
     * @param password 密码
     * @return
     */
    public AccountPo login(String username, String password) {
        AccountPo Account = getAccountByUsername(username);
        Assert.notNull(Account, ResultTo.getCodeAndMsg(ResultTo.ResultCode.ACC_LOGIN_FAIL));
        Assert.isTrue(Account.getPassword().equals(SecretUtil.encryptPwd(password, Account.getSlat())), ResultTo.getCodeAndMsg(ResultTo.ResultCode.ACC_LOGIN_FAIL));
        Assert.isTrue(StatusTypeEnums.StatusEnums.ENABLE.equals(Account.getStatus()), ResultTo.getCodeAndMsg(ResultTo.ResultCode.ACC_USER_DISABLE));
        return Account;
    }

    public AccountPo getAccountByUsername(String username) {
        return accountMapper.selectOne(new QueryWrapper<AccountPo>().lambda().eq(AccountPo::getUsername, username));
    }
}
